import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('@/layouts/MainLayout.vue'),
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '仪表盘' }
      },
      {
        path: 'activity/list',
        name: 'ActivityList',
        component: () => import('@/views/activity/ActivityList.vue'),
        meta: { title: '活动列表' }
      },
      {
        path: 'activity/detail/:id',
        name: 'ActivityDetail',
        component: () => import('@/views/activity/ActivityDetail.vue'),
        meta: { title: '活动详情' }
      },
      {
        path: 'activity/create',
        name: 'ActivityCreate',
        component: () => import('@/views/activity/ActivityCreate.vue'),
        meta: { title: '创建活动', requiresAdmin: true }
      },
      {
        path: 'activity/manage',
        name: 'ActivityManage',
        component: () => import('@/views/activity/ActivityManage.vue'),
        meta: { title: '活动管理', requiresAdmin: true }
      },
      {
        path: 'activity/my',
        name: 'MyActivities',
        component: () => import('@/views/activity/MyActivities.vue'),
        meta: { title: '我的活动' }
      },
      {
        path: 'demand/list',
        name: 'DemandList',
        component: () => import('@/views/demand/DemandList.vue'),
        meta: { title: '需求列表' }
      },
      {
        path: 'demand/detail/:id',
        name: 'DemandDetail',
        component: () => import('@/views/demand/DemandDetail.vue'),
        meta: { title: '需求详情' }
      },
      {
        path: 'demand/publish',
        name: 'DemandPublish',
        component: () => import('@/views/demand/DemandPublish.vue'),
        meta: { title: '发布需求', requiresAdmin: true }
      },
      {
        path: 'point',
        name: 'Point',
        component: () => import('@/views/Point.vue'),
        meta: { title: '我的积分' }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/Profile.vue'),
        meta: { title: '个人中心' }
      },
      {
        path: 'notification/list',
        name: 'NotificationList',
        component: () => import('@/views/notification/NotificationList.vue'),
        meta: { title: '通知公告' }
      },
      {
        path: 'notification/manage',
        name: 'NotificationManage',
        component: () => import('@/views/notification/NotificationManage.vue'),
        meta: { title: '公告管理', requiresAdmin: true }
      },
      {
        path: 'statistics/overview',
        name: 'StatisticsOverview',
        component: () => import('@/views/statistics/Overview.vue'),
        meta: { title: '数据统计', requiresAdmin: true }
      },
      {
        path: 'system/user',
        name: 'UserManage',
        component: () => import('@/views/system/UserManage.vue'),
        meta: { title: '用户管理', requiresAdmin: true }
      },
      {
        path: 'volunteer/list',
        name: 'VolunteerList',
        component: () => import('@/views/volunteer/VolunteerList.vue'),
        meta: { title: '志愿者列表' }
      },
      {
        path: 'volunteer/my-info',
        name: 'MyInfo',
        component: () => import('@/views/volunteer/MyInfo.vue'),
        meta: { title: '我的信息' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 如果需要认证但未登录，跳转到登录页
  if (to.meta.requiresAuth !== false) {
    if (!userStore.token) {
      next('/login')
      return
    }
  }

  // 如果需要管理员权限但用户不是管理员
  if (to.meta.requiresAdmin && !userStore.isAdmin()) {
    window.$message?.error('没有权限访问该页面')
    // 如果从登录页来的，跳转到仪表盘，否则返回上一页或仪表盘
    if (from.path === '/login' || !from.path || from.path === to.path) {
      next('/dashboard')
    } else {
      next(false) // 取消导航
    }
    return
  }

  next()
})

export default router

